package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.business.domain.Bid;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.PaymentSchedule;
import cn.wolfcode.p2p.business.domain.PaymentScheduleDetail;
import cn.wolfcode.p2p.business.mapper.PaymentScheduleDetailMapper;
import cn.wolfcode.p2p.business.service.IPaymentScheduleDetailService;
import cn.wolfcode.p2p.util.CalculatetUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class PaymentScheduleDetailServiceImpl implements IPaymentScheduleDetailService {

    @Autowired
    private PaymentScheduleDetailMapper paymentScheduleDetailMapper;


    @Override
    public List<PaymentScheduleDetail> collectionPlan(PaymentSchedule paymentSchedule, List<Bid> bids, BidRequest bidRequest) {
        List<PaymentScheduleDetail> paymentScheduleDetails = new ArrayList<>();
        BigDecimal totalInterestTemp  = new BigDecimal("0.000");
        BigDecimal totalPrincipalTemp  = new BigDecimal("0.000");
        for (int i = 0; i < bids.size(); i++) {
            Bid bid = bids.get(i);
            BigDecimal availableAmount = bid.getAvailableAmount();
            PaymentScheduleDetail paymentScheduleDetail = new PaymentScheduleDetail();
            paymentScheduleDetail.setBidAmount(availableAmount);
            paymentScheduleDetail.setBidId(bid.getId());
            paymentScheduleDetail.setMonthIndex(paymentSchedule.getMonthIndex());
            paymentScheduleDetail.setDeadline(paymentSchedule.getDeadline());
            paymentScheduleDetail.setBidRequestId(bid.getBidRequestId());
            paymentScheduleDetail.setReturnType(paymentSchedule.getReturnType());
            paymentScheduleDetail.setPaymentScheduleId(paymentSchedule.getId());
            paymentScheduleDetail.setFromLoginInfo(paymentSchedule.getBorrowUser());
            paymentScheduleDetail.setToLoginInfo(bid.getBidUser());
            if(i == bids.size() - 1){
                paymentScheduleDetail.setPrincipal(paymentSchedule.getPrincipal().subtract(totalPrincipalTemp));
                paymentScheduleDetail.setInterest(paymentSchedule.getInterest().subtract(totalInterestTemp));
                paymentScheduleDetail.setTotalAmount(paymentScheduleDetail.getInterest().add(paymentScheduleDetail.getPrincipal()));
            }else{
                //设置每月应收本金
                BigDecimal monthlyReceivePrincipal = CalculatetUtil.calMonthlyReceivePrincipal(availableAmount,bidRequest.getBidRequestAmount(),paymentSchedule.getPrincipal());
                totalPrincipalTemp = totalPrincipalTemp.add(monthlyReceivePrincipal);
                paymentScheduleDetail.setPrincipal(monthlyReceivePrincipal);
                //设置每月应收利息
                BigDecimal monthlyReceiveInterest = CalculatetUtil.calMonthlyReceiveInterest(availableAmount, bidRequest.getBidRequestAmount(), paymentSchedule.getInterest());
                totalInterestTemp = totalInterestTemp.add(monthlyReceiveInterest);
                paymentScheduleDetail.setInterest(monthlyReceiveInterest);
                ////设置每月应收总额
                paymentScheduleDetail.setTotalAmount(monthlyReceivePrincipal.add(monthlyReceiveInterest));
            }
            paymentScheduleDetailMapper.insert(paymentScheduleDetail);
            paymentScheduleDetails.add(paymentScheduleDetail);

        }
        return paymentScheduleDetails;
    }

    @Override
    public void upadtePayDate(PaymentScheduleDetail paymentScheduleDetail) {
        paymentScheduleDetailMapper.upadtePayDate(paymentScheduleDetail);
    }

    @Override
    public void updateTransferStateByBidIdAndUserId(Long bidId, Long userId, boolean transferState) {
        paymentScheduleDetailMapper.updateTransferStateByBidIdAndUserId(bidId,userId,transferState);
    }

    @Override
    public void updateTransferStateAndTranToByBidIdAndUserId(Long bidId, Long transFromId, Long transToId, boolean transferState) {
        paymentScheduleDetailMapper.updateTransferStateAndTranToByBidIdAndUserId(bidId,transFromId,transToId,transferState);
    }

}
